package winOrDie.graphicsInterface.GameRoom;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

/**
 * This public class is a {@link Composite} that contains Game room chat
 *
 */
public class Composite_Chat extends Composite {
	
	private Composite composite_Grid = null;
	private Composite composite_Text_Output = null;
	private Text text_Output = null;
	private Composite composite_Input_Chat = null;
	private Text text_Input_Chat = null;
	private Button button_Input_Chat = null;
	
	
	
	private Vector<Composite_Chat_Listener> listeners = new Vector<Composite_Chat_Listener>();
	
	public synchronized void add_Composite_Chat_Listener(Composite_Chat_Listener listener)
	{
		listeners.addElement(listener);
	}
	
	public synchronized void remove_Composite_Chat_Listener(Composite_Chat_Listener listener)
	{
		listeners.removeElement(listener);
	}
	
	protected void fireEvent(Composite_Chat_Event event)
	{
		for (Composite_Chat_Listener listener : listeners) {
			listener.on_Composite_Chat_Changed(event);
		}
	}
	
	
	
	public Composite_Chat(Composite parent, int style) {
		super(parent, style);
		initialize();
	}

	private void initialize() {
		
		setLayout(new FillLayout());
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		composite_Grid = new Composite(this, SWT.NONE);
		composite_Grid.setLayout(gridLayout);
		
		
		
		
	
		get_Text_Output();
		get_Text_Input_Chat();
	}
	
	
	public Text get_Text_Output()
	{
		
		if (text_Output == null)
		{
			GridData gridData = new GridData();
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			gridData.horizontalAlignment = SWT.FILL;
			gridData.verticalAlignment = SWT.FILL;
			
			composite_Text_Output = new Composite(composite_Grid, SWT.BORDER);
			composite_Text_Output.setLayout(new FillLayout());
			composite_Text_Output.setLayoutData(gridData);
			
			text_Output = new Text(composite_Text_Output, 
					SWT.MULTI | 
					SWT.V_SCROLL);
			//text_Output.setLayoutData(gridData);
		}

		return text_Output;
	}
	
	
	public Text get_Text_Input_Chat()
	{
		
		if (text_Input_Chat == null)
		{
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			
			GridData gridData_Input_Chat = new GridData();
			gridData_Input_Chat.grabExcessHorizontalSpace = true;
			gridData_Input_Chat.horizontalAlignment = SWT.FILL;
			
			composite_Input_Chat = new Composite(composite_Grid, SWT.NONE);
			composite_Input_Chat.setLayout(gridLayout);
			composite_Input_Chat.setLayoutData(gridData_Input_Chat);
			
			GridData gridData_Text_Input_Chat = new GridData();
			gridData_Text_Input_Chat.grabExcessHorizontalSpace = true;
			gridData_Text_Input_Chat.horizontalAlignment = SWT.FILL;
			text_Input_Chat = new Text(composite_Input_Chat, SWT.BORDER);
			text_Input_Chat.setLayoutData(gridData_Text_Input_Chat);
			
			
			GridData gridData_Button_Input_Chat = new GridData();
			gridData_Button_Input_Chat.grabExcessHorizontalSpace = false;
			
			button_Input_Chat = new Button(composite_Input_Chat, SWT.PUSH);
			button_Input_Chat.setLayoutData(gridData_Button_Input_Chat);
			
			button_Input_Chat.setText("Enviar");
			
			
			final Composite_Chat compositeChat = this;
			button_Input_Chat.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Chat_Event event = new Composite_Chat_Event(compositeChat);
					event.setEventType(Composite_Chat_Event.Event_Types.SUBMIT);
					fireEvent(event);
				}
			});
		}

		return text_Input_Chat;
		
	}
	
	
}
